/**
 * Copyright (c) 2002-2008 jNETx.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNETx. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNETx.
 *
 * $Id: $
 *
 */
package com.pas.book;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class TextDrawer {

    private final RasterFont font;

    public TextDrawer(RasterFont font) {
        this.font = font;
    }

    public void draw(Graphics g, String str, int x, int y) {
        int maxX = g.getClipX() + g.getClipWidth();
        for (int i = 0; x < maxX && i < str.length(); i++) {
            x += draw(g, str.charAt(i), x, y);
        }
    }

    public void draw(Graphics g, char[] chars, int x, int y) {
        draw(g, chars, 0, chars.length, x, y);
    }

    public void draw(Graphics g, char[] chars, int from, int length, int x, int y) {
        int maxX = g.getClipX() + g.getClipWidth();
        for (int i = from; x < maxX && i < from + length; i++) {
            x += draw(g, chars[i], x, y);
        }
    }

    public int draw(Graphics g, char ch, int x, int y) {
        Image img = font.getGlyph(ch);
        if (img != null) {
            g.drawImage(img, x, y, Graphics.TOP | Graphics.LEFT);
            return img.getWidth();
        }
        return 0;
    }

    public RasterFont getFont() {
        return font;
    }
}
